package com.frejoys.store.controller;

import com.frejoys.common.aop.Authority;
import com.frejoys.common.aop.Authorization;
import com.frejoys.common.constant.CommonConstant;
import com.frejoys.common.dto.common.IdDto;
import com.frejoys.common.dto.common.OrderSnDto;
import com.frejoys.common.dto.order.OrderListDto;
import com.frejoys.common.dto.order.OrderStorePageDto;
import com.frejoys.common.dto.store.OrderSnVerifyDto;
import com.frejoys.common.error.HttpStatus;
import com.frejoys.common.exception.GeneralException;
import com.frejoys.common.response.Result;
import com.frejoys.common.util.CacheKeyUtil;
import com.frejoys.common.util.RequestUtil;
import com.frejoys.common.vo.order.*;
import com.frejoys.common.vo.store.ScanOrderInfoVo;
import com.frejoys.common.vo.store.StoreVo;
import com.frejoys.service.OrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Tag(name = "订单", description = "订单")
@RestController
@RequiredArgsConstructor
@RequestMapping(CommonConstant.routePrefixStore)
@Authorization(Authority.Private)
public class MallOrderController {

    private final OrderService orderService;

    private final RedissonClient redissonClient;


    @Operation(summary = "商家扫码查看用户需核销订单信息")
    @PostMapping("/order/scan/info")
    public Result<ScanOrderInfoVo> orderScanInfo(@RequestBody @Validated OrderSnDto dto) {
        return Result.success(orderService.getStoreOrderInfoByOrderSn(dto.getOrderSn(), RequestUtil.userId()));
    }

    @Operation(summary = "订单核销")
    @PostMapping("/order/verify")
    public Result<List<StoreVo>> orderVerify(@RequestBody @Validated OrderSnVerifyDto dto) {

        RLock lock = redissonClient.getLock(CacheKeyUtil.orderVerifyLockCacheKey(dto.getId()));

        if (lock.tryLock()) {
            try {

                orderService.orderVerify(RequestUtil.userId(), dto.getId(), dto.getOrderSn());

            } finally {
                if (lock.isLocked() && lock.isHeldByCurrentThread()) {
                    lock.unlock();
                }
            }
        } else {
            throw new GeneralException(HttpStatus.beingProcessed);
        }

        return Result.success();
    }

    /**
     * 商家端分页查询订单数据 待注释
     */
//    @Authorization(Authority.Protected)
//    @Operation(summary = "分页查询订单数据")
//    @PostMapping("/order/page")
//    public Result<List<StoreOrderPageVo>> pageOrder(@RequestBody @Validated OrderStorePageDto dto) {
//        return Result.success(orderService.pageOrder(dto, RequestUtil.userId()));
//    }

    /**
     * 商家端查询订单详情 待注释
     */
//    @Operation(summary = "查询订单详情")
//    @PostMapping("/order/getInfo")
//    public Result<StoreOrderInfoVo> getInfoByStore(@RequestBody @Validated IdDto idDto) {
//        return Result.success(orderService.getInfoByStore(idDto));
//    }



    //新接口
    @Authorization(Authority.Protected)
    @Operation(summary = "查询订单列表")
    @PostMapping("/order/orderListInfo")
    public Result<OrderAndGoodsListVo> orderListInfo(@RequestBody @Validated OrderListDto dto) {
        return Result.success(orderService.storeOrderListInfo(dto,RequestUtil.userId()));
    }


    //新接口
    @Operation(summary = "查询订单详情")
    @PostMapping("/order/getStoreByOrder")
    public Result<StoreOrderVo> getStoreByOrder(@RequestBody @Validated IdDto dto) {
        return Result.success(orderService.getStoreByOrder(dto));
    }

}
